package com.small.commom.filetools;

/**
 * 锟斤拷锟斤拷锟斤拷锟斤拷锟叫憋拷
static List<File>	
GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)

  

 取锟斤拷压锟斤拷锟斤拷锟叫碉拷锟侥硷拷锟叫憋拷(锟侥硷拷锟斤拷,锟侥硷拷锟斤拷选)

 zipFileString      : 压锟斤拷锟斤拷锟侥硷拷

 bContainFolder : 锟角凤拷锟斤拷锟斤拷募锟斤拷锟�

 bContainFile      : 锟角凤拷锟斤拷锟斤拷募锟�
static InputStream	
UpZip(String zipFileString, String fileString)

  

 锟斤拷锟斤拷压锟斤拷锟斤拷锟叫碉拷锟侥硷拷InputStream

 zipFileString : 压锟斤拷锟斤拷锟侥硷拷

 fileString      : 要锟斤拷压锟斤拷锟侥硷拷锟斤拷
static void	
UnZipFolder(String zipFileString, String outPathString)

  

 锟斤拷压一锟斤拷压锟斤拷锟侥碉拷 锟斤拷指锟斤拷位锟斤拷

 zipFileString    : 压锟斤拷锟斤拷锟侥硷拷

 outPathString : 要锟斤拷锟斤拷锟铰凤拷锟�
static void	
ZipFolder(String srcFileString, String zipFileString)

  

 压锟斤拷锟侥硷拷,锟侥硷拷锟斤拷

 srcFileString : 要压锟斤拷锟斤拷锟侥硷拷,锟侥硷拷锟叫碉拷路锟斤拷

 zipFileString : 锟斤拷锟窖癸拷锟斤拷锟斤拷路锟斤拷
 * @author Sun
 *
 */
public class MyZip {  
  
    public MyZip(){  
  
    }  
      
    /** 
     * 取锟斤拷压锟斤拷锟斤拷锟叫碉拷 锟侥硷拷锟叫憋拷(锟侥硷拷锟斤拷,锟侥硷拷锟斤拷选) 
     * @param zipFileString     压锟斤拷锟斤拷锟斤拷锟斤拷 
     * @param bContainFolder    锟角凤拷锟斤拷锟�锟侥硷拷锟斤拷 
     * @param bContainFile      锟角凤拷锟斤拷锟�锟侥硷拷 
     * @return 
     * @throws Exception 
     */  
    public static java.util.List<java.io.File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {  
          
        android.util.Log.v("XZip", "GetFileList(String)");  
          
        java.util.List<java.io.File> fileList = new java.util.ArrayList<java.io.File>();  
        java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));  
        java.util.zip.ZipEntry zipEntry;  
        String szName = "";  
          
        while ((zipEntry = inZip.getNextEntry()) != null) {  
            szName = zipEntry.getName();  
          
            if (zipEntry.isDirectory()) {  
          
                // get the folder name of the widget  
                szName = szName.substring(0, szName.length() - 1);  
                java.io.File folder = new java.io.File(szName);  
                if (bContainFolder) {  
                    fileList.add(folder);  
                }  
          
            } else {  
                java.io.File file = new java.io.File(szName);  
                if (bContainFile) {  
                    fileList.add(file);  
                }  
            }  
        }//end of while  
          
        inZip.close();  
          
        return fileList;  
    }  
  
    /** 
     * 锟斤拷锟斤拷压锟斤拷锟斤拷锟叫碉拷锟侥硷拷InputStream 
     * @param zipFileString     压锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷 
     * @param fileString    锟斤拷压锟侥硷拷锟斤拷锟斤拷锟斤拷 
     * @return InputStream 
     * @throws Exception 
     */  
    public static java.io.InputStream UpZip(String zipFileString, String fileString)throws Exception {  
//        android.util.Log.v("XZip", "UpZip(String, String)");
    	  android.util.Log.v("zipFileString", zipFileString);  
    	  android.util.Log.v("fileString", fileString);  
        java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(zipFileString);  
        java.util.zip.ZipEntry zipEntry = zipFile.getEntry(fileString);  
          
        return zipFile.getInputStream(zipEntry);   
    }  
      
      
    /** 
     * 锟斤拷压一锟斤拷压锟斤拷锟侥碉拷 锟斤拷指锟斤拷位锟斤拷 
     * @param zipFileString 压锟斤拷锟斤拷锟斤拷锟斤拷锟�
     * @param outPathString 指锟斤拷锟斤拷路锟斤拷 
     * @throws Exception 
     */  
    public static void UnZipFolder(String zipFileString, String outPathString)throws Exception {  
        android.util.Log.v("XZip", "UnZipFolder(String, String)");  
        java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));  
        java.util.zip.ZipEntry zipEntry;  
        String szName = "";  
          
        while ((zipEntry = inZip.getNextEntry()) != null) {  
            szName = zipEntry.getName();  
          
            if (zipEntry.isDirectory()) {  
          
                // get the folder name of the widget  
                szName = szName.substring(0, szName.length() - 1);  
                java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);  
                folder.mkdirs();  
          
            } else {  
          
                java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);  
                file.createNewFile();  
                // get the output stream of the file  
                java.io.FileOutputStream out = new java.io.FileOutputStream(file);  
                int len;  
                byte[] buffer = new byte[1024];  
                // read (len) bytes into buffer  
                while ((len = inZip.read(buffer)) != -1) {  
                    // write (len) byte from buffer at the position 0  
                    out.write(buffer, 0, len);  
                    out.flush();  
                }  
                out.close();  
            }  
        }//end of while  
          
        inZip.close();  
      
    }//end of func  
      
  
    /** 
     * 压锟斤拷锟侥硷拷,锟侥硷拷锟斤拷 
     * @param srcFileString 要压锟斤拷锟斤拷锟侥硷拷/锟侥硷拷锟斤拷锟斤拷锟斤拷 
     * @param zipFileString 指锟斤拷压锟斤拷锟斤拷目锟侥猴拷锟斤拷锟斤拷 
     * @throws Exception 
     */  
    public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {  
        android.util.Log.v("XZip", "ZipFolder(String, String)");  
          
        //锟斤拷锟斤拷Zip锟斤拷  
        java.util.zip.ZipOutputStream outZip = new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(zipFileString));  
          
        //锟斤拷要锟斤拷锟斤拷锟斤拷募锟� 
        java.io.File file = new java.io.File(srcFileString);  
  
        //压锟斤拷  
        ZipFiles(file.getParent()+java.io.File.separator, file.getName(), outZip);  
          
        //锟斤拷锟�锟截憋拷  
        outZip.finish();  
        outZip.close();  
      
    }//end of func  
      
    /** 
     * 压锟斤拷锟侥硷拷 
     * @param folderString 
     * @param fileString 
     * @param zipOutputSteam 
     * @throws Exception 
     */  
    private static void ZipFiles(String folderString, String fileString, java.util.zip.ZipOutputStream zipOutputSteam)throws Exception{  
        android.util.Log.v("XZip", "ZipFiles(String, String, ZipOutputStream)");  
          
        if(zipOutputSteam == null)  
            return;  
          
        java.io.File file = new java.io.File(folderString+fileString);  
          
        //锟叫讹拷锟角诧拷锟斤拷锟侥硷拷  
        if (file.isFile()) {  
  
            java.util.zip.ZipEntry zipEntry =  new java.util.zip.ZipEntry(fileString);  
            java.io.FileInputStream inputStream = new java.io.FileInputStream(file);  
            zipOutputSteam.putNextEntry(zipEntry);  
              
            int len;  
            byte[] buffer = new byte[4096];  
              
            while((len=inputStream.read(buffer)) != -1)  
            {  
                zipOutputSteam.write(buffer, 0, len);  
            }  
              
            zipOutputSteam.closeEntry();  
        }  
        else {  
              
            //锟侥硷拷锟叫的凤拷式,锟斤拷取锟侥硷拷锟斤拷锟铰碉拷锟斤拷锟侥硷拷  
            String fileList[] = file.list();  
              
            //锟斤拷锟矫伙拷锟斤拷锟斤拷募锟� 锟斤拷锟斤拷咏锟饺ワ拷锟斤拷锟� 
            if (fileList.length <= 0) {  
                java.util.zip.ZipEntry zipEntry =  new java.util.zip.ZipEntry(fileString+java.io.File.separator);  
                zipOutputSteam.putNextEntry(zipEntry);  
                zipOutputSteam.closeEntry();                  
            }  
              
            //锟斤拷锟斤拷锟斤拷锟斤拷募锟� 锟斤拷锟斤拷锟斤拷锟侥硷拷  
            for (int i = 0; i < fileList.length; i++) {  
                ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);  
            }//end of for  
      
        }//end of if  
          
    }//end of func  
      
    public void finalize() throws Throwable {  
          
    }  
  
}  